/*
 * Created by IntelliJ IDEA.
 * User: mark
 * Date: 10/03/2004
 * Time: 13:34:00
 */
package com.talios.jira.browser;

import com.intellij.openapi.diagnostic.Logger;
import com.talios.jira.JiraException;
import com.talios.jira.browser.signals.JiraServerDetails;
import com.talios.jira.idea.JDomSupport;
import com.talios.jira.rpc.JiraRpcClient;
import org.jdom.Element;

import java.util.List;

public class JiraProjectDetails {
    private static final Logger LOGGER = Logger.getInstance("Jira Browser");

    private String projectId;
    private String projectName;
    private String projectKey;
    private String componentId;
    private String componentName;

    private JiraServerDetails server;
    private List allVersions;
    private List unreleasedVersions;
    private List components;

    public String getProjectId() {
        return projectId;
    }

    public void setProjectId(String projectId) {
        this.projectId = projectId;
    }

    public String getComponentId() {
        return componentId;
    }

    public void setComponentId(String componentId) {
        this.componentId = componentId;
    }

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    public String getProjectKey() {
        return projectKey;
    }

    public void setProjectKey(String projectKey) {
        this.projectKey = projectKey;
    }

    public String getComponentName() {
        return componentName;
    }

    public void setComponentName(String componentName) {
        this.componentName = componentName;
    }

    public JiraProjectDetails(JiraServerDetails server) {
        this.server = server;
        projectId = "";
        componentId = "-1";
    }

    public void writeExternal(Element projectDetail) {
        if (getProjectId() != null) {
            projectDetail.setAttribute("projectid", getProjectId());
        }
        if (getProjectName() != null) {
            projectDetail.setAttribute("projectname", getProjectName());
        }
        if (getProjectKey() != null) {
            projectDetail.setAttribute("projectkey", getProjectKey());
        }
        if (getComponentId() != null) {
            projectDetail.setAttribute("componentid", getComponentId());
        }
        if (getComponentName() != null) {
            projectDetail.setAttribute("componentname", getComponentName());
        }
    }

    public void readExternal(Element projectElement) {
        setProjectId(JDomSupport.getAttributeWithDefault(projectElement, "projectid", ""));
        setProjectName(JDomSupport.getAttributeWithDefault(projectElement, "projectname", ""));
        setProjectKey(JDomSupport.getAttributeWithDefault(projectElement, "projectkey", ""));
        setComponentId(JDomSupport.getAttributeWithDefault(projectElement, "componentid", ""));
        setComponentName(JDomSupport.getAttributeWithDefault(projectElement, "componentname", ""));
    }

    public JiraServerDetails getServer() {
        return server;
    }

    public void refreshLists() {
        try {
            if (projectKey != null) {
                JiraRpcClient rpcClient = server.getRpcClient();
                allVersions = rpcClient.getVersions(projectKey);
                unreleasedVersions = rpcClient.getUnreleasedVersions(projectKey);
                components = rpcClient.getComponents(projectKey);
            }
        } catch (JiraException e) {
            LOGGER.error("Unable to update project versions.", e);
        }
    }

    public List getAllVersions() {
        return allVersions;
    }

    public JiraListComboBoxModel getAllVersionsModel() {
        return new JiraListComboBoxModel(allVersions);
    }

    public List getUnreleasedVersions() {
        return unreleasedVersions;
    }

    public JiraListComboBoxModel getUnreleasedVersionsModel() {
        return new JiraListComboBoxModel(unreleasedVersions);
    }

    public List getComponents() {
        return components;
    }

    public JiraListComboBoxModel getComponentsModel() {
        return new JiraListComboBoxModel(components);
    }

}